/**
 * 登录状态处理工具
 */
export const LoginStatusHandler = {
	/**
	 * 状态码映射
	 */
	STATUS_CODES: {
		NOT_BIND: '50001', // 未实名认证
		NOT_CHECK: '50002', // 待审核
		NOT_PASS: '50003', // 审核未通过
	},

	/**
	 * 状态信息映射
	 */
	STATUS_INFO: {
		50001: {
			title: '您尚未实名制认证！',
			canBind: true,
			canRefresh: false,
			buttonText: '去认证',
			routePath: '/company/bound',
		},
		50002: {
			title: '您的实名制认证尚未审核完毕，请稍等！',
			canBind: false,
			canRefresh: true,
			buttonText: '刷新',
			routePath: null,
		},
		50003: {
			title: '您的实名制认证未审核通过！',
			canBind: false,
			canRefresh: false,
			buttonText: null,
			routePath: null,
		},
	},

	/**
	 * 检查是否是特殊状态码
	 * @param {string} code 状态码
	 * @returns {boolean}
	 */
	isSpecialStatusCode(code) {
		return Object.values(this.STATUS_CODES).includes(code)
	},

	/**
	 * 获取状态信息
	 * @param {string} code 状态码
	 * @returns {object}
	 */
	getStatusInfo(code) {
		return (
			this.STATUS_INFO[code] || {
				title: '未知状态',
				canBind: false,
				canRefresh: false,
				buttonText: null,
				routePath: null,
			}
		)
	},

	/**
	 * 获取处理动作
	 * @param {string} code 状态码
	 * @returns {string}
	 */
	getActionType(code) {
		switch (code) {
			case this.STATUS_CODES.NOT_BIND:
				return 'navigate'
			case this.STATUS_CODES.NOT_CHECK:
				return 'refresh'
			case this.STATUS_CODES.NOT_PASS:
				return 'none'
			default:
				return 'none'
		}
	},
}

/**
 * 登录结果处理器
 */
export class LoginResultHandler {
	constructor(router, userStore) {
		this.router = router
		this.userStore = userStore
	}

	/**
	 * 处理登录结果
	 * @param {object} result 登录结果
	 * @param {object} options 选项
	 * @returns {Promise<object>}
	 */
	async handleResult(result, options = {}) {
		const { onSuccess, onSpecialStatus, onError, defaultRedirect = '/' } = options

		if (result.success) {
			// 登录成功
			if (onSuccess) {
				return await onSuccess(result.data)
			}

			// 默认跳转到首页或指定页面
			const redirect = this.router.currentRoute.value.query.redirect || defaultRedirect
			this.router.replace(redirect)

			return { type: 'success', data: result.data }
		} else if (result.needSpecialHandle && result.code) {
			// 特殊状态码处理
			if (onSpecialStatus) {
				return await onSpecialStatus(result.code, result.msg)
			}

			return this.handleSpecialStatus(result.code, result.msg)
		} else if (result.needAuth) {
			// 需要OAuth授权
			return { type: 'needAuth' }
		} else {
			// 其他错误
			if (onError) {
				return await onError(result.error || '登录失败')
			}

			return {
				type: 'error',
				message: result.error || '登录失败',
			}
		}
	}

	/**
	 * 处理特殊状态
	 * @param {string} code 状态码
	 * @param {string} message 消息
	 * @returns {object}
	 */
	handleSpecialStatus(code, message) {
		const statusInfo = LoginStatusHandler.getStatusInfo(code)
		const actionType = LoginStatusHandler.getActionType(code)

		return {
			type: 'specialStatus',
			code,
			message: message || statusInfo.title,
			statusInfo,
			actionType,
		}
	}

	/**
	 * 执行特殊状态的动作
	 * @param {string} code 状态码
	 */
	executeAction(code) {
		const actionType = LoginStatusHandler.getActionType(code)
		const statusInfo = LoginStatusHandler.getStatusInfo(code)

		switch (actionType) {
			case 'navigate':
				if (statusInfo.routePath) {
					this.router.push(statusInfo.routePath)
				}
				break
			case 'refresh':
				window.location.reload()
				break
			case 'none':
			default:
				// 不执行任何动作
				break
		}
	}
}

export default {
	LoginStatusHandler,
	LoginResultHandler,
}
